home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 13611 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.6 KB

  1. Path: news.sprintlink.net!datalytics!usenet
  2. From: Rob Stewart <stew@datalytics.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Inheritance problem...and other stuff
  5. Date: Tue, 26 Mar 1996 10:37:43 -0500
  6. Organization: Datalytics, Inc
  7. Message-ID: <31580F47.530B@datalytics.com>
  8. References: <199603232309.HAA23168@public1.guangzhou.gd.cn>
  9. NNTP-Posting-Host: 204.62.224.71
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (WinNT; I)
  14.  
  15. Wang TianXing wrote:
  16. > On 23 Mar 1996 14:49:22 GMT, grantp@usa.pipeline.com(Pete Grant)
  17. > wrote:
  18. > | On Mar 23, 1996 09:01:07 in article <Inheritance problem...and other
  19. > | stuff>, 'pcgpe@ix.netcom.com(Mark Feldman)' wrote:
  20. > |
  21. > | >2nd Question:
  22. > | >
  23. > | >What's the standard way of storing such an array to a file and
  24. > | >retrieving it? I could always assign an ID number to each class type
  25. > | >and load them using case statements but this seems messy. I notice that
  26. > | >MFC and OWL seem to use tables to handle the dispatching of windows
  27. > | >messages. Would it be feasable to use this for saving the array info to
  28. > | >a file? Idealy I would like each function to inherit a serialization
  29. > | >function which they can overload to save and restore their data, but
  30. > | >how can I make the loading routine know what type of class to create in
  31. > | >the first place while still keeping the code tight and clean?
  32. > | >
  33. > | There's only one way:  store the type information in the file.  An
  34. > | enum is probably best; alternately, a name string would work also.
  35. > | When you read the data back in, you must check the type info and
  36. > | create an object of appropriate type.
  37. > |
  38. > | There are standard idioms (Coplien's book, for example) for simulating
  39. > | virtual constructors -- I have mixed feelings of those since they
  40. > | are just a thin veil for hiding what you are really doing.  There's
  41. > | really no such thing as a virtual constructor in C++ and I feel a bit
  42. > | uneasy with idioms that try to make it look like they exist.  But
  43. > | at the same time, I can see the benefits also.
  44.  
  45. Thinly veiled or not, anything that encapsulates what you're 
  46. doing is valuable.  For example, the age-old C++ problem of a 
  47. class hierarchy having a virtual function that returns a base 
  48. class pointer despite the class in which it is used is a pain.  
  49. Using an inline mf to encapsulate the inevitable cast to the 
  50. derived type is useful.  It eliminates having to write the cast 
  51. everywhere.  (In fact, when your compiler supports the new casts 
  52. and rtti, your inline function can take advantage of that to 
  53. validate its casting.  Of course, when your compiler supports 
  54. that, it might support the new ability to return the derived 
  55. type pointer while still overriding the virtual function in the 
  56. base class!)
  57.  
  58. As for using an idiom to simulate what is not in the language is 
  59. hardly new.  For example, C/C++ don't support linked lists.  
  60. Does that mean we shouldn't implement them?  Of course not.  If 
  61. your problem is with calling these things "constructors," and 
  62. you consider that constructors are specifically defined by the 
  63. language, then give them another name.  Once the semantics are 
  64. out of the way, you'll have a useful idiom available for your 
  65. use when you need it.
  66.  
  67. > In Borland C++, there is a Streamable Class hierarchy, which does what
  68. > Mark wants and even more.  Search TStreamable in the online help.
  69.  
  70. MFC does this with its Serialize function in classes derived 
  71. from CObject.  Rogue Wave's Tools.h++ does this with saveGuts 
  72. and restoreGuts in RWCollectable.
  73.  
  74. > ---
  75. > Wang TianXing
  76.  
  77. -- 
  78. Robert Stewart        | My opinions are usually my own.
  79. Datalytics, Inc.    | stew@datalytics.com
  80.